<?php
/**
* 每日步数模型
* @DateTime 2018-06-04
* @author xyl
*/
class Step_model extends MY_Model
{

	private $table='step_record';

	function __construct()
	{
		parent::__construct();

	}

	/**
	 * 获取今天的步数
	 * @author XYL
	 * @DateTime 2018-06-04
	 * @param    [type]     $user_id [description]
	 * @return   [type]              [description]
	 */
	public function get_today($user_id)
	{
		$where = array(
			'del_status'	=>	0,
			'user_id'	=>	$user_id,
			'date'		=>	date('Y-m-d 00:00:00'),
		);
		$info = $this->hw_getRecord($this->table,$where);
		return $info;
	}

	/**
	 * 获取今日排行榜
	 * @author XYL
	 * @DateTime 2018-06-04
	 * @return   [type]     [description]
	 */
	public function get_totay_rank()
	{
		$where = array(
			'del_status'	=>	0,
			'is_online'		=>	1,
			'date'		=>	date('Y-m-d 00:00:00'),
		);
		$list = $this->hw_getList($this->table,$where,'*','step desc');
		$data = array();
		if (!empty($list)) {
			foreach ($list as $key => &$value) {
				$info = $this->hw_getRecord('user',array('id'=>$value['user_id']));
				$data[] = array(
					'headimgurl'	=>	$info['headimgurl'],
					'nickname'	=>	$info['nickname'],
					'step'	=>	$value['step'],
				);	 
			}
		}
		return $data;
	}

	/**
	 * 获取本周排行榜
	 * @author XYL
	 * @DateTime 2018-06-04
	 * @return   [type]     [description]
	 */
	public function get_week_rank()
	{
		$where = array(
			'del_status'	=>	0,
			'is_online'		=>	1,
			'date>='		=>	date('Y-m-d 00:00:00', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)),
			'date<='		=>	date('Y-m-d 00:00:00'),
		);
		$list = $this->db->where($where)->select('*,sum(step) as step_num')->group_by('user_id')->order_by('step_num desc')->get($this->table)->result_array();
		$data = array();
		if (!empty($list)) {
			foreach ($list as $key => &$value) {
				$info = $this->hw_getRecord('user',array('id'=>$value['user_id']));
				$data[] = array(
					'headimgurl'	=>	$info['headimgurl'],
					'nickname'	=>	$info['nickname'],
					'step'	=>	$value['step_num'],
				);	 
			}
		}
		return $data;
	}

	/**
	 * 获取
	 * @author XYL
	 * @DateTime 2018-06-04
	 * @param    [type]     $user_id [description]
	 * @return   [type]              [description]
	 */
	public function get_sort($user_id)
	{
		$where = array(
			'del_status'	=>	0,
			'is_online'		=>	1,
			'date'		=>	date('Y-m-d 00:00:00'),
		);
		$list = $this->hw_getList($this->table,$where,'*','step desc');
		$offset = 0;
		foreach ($list as $key => $value) {
			if ($value['user_id']==$user_id) {
				$offset = $key;break;
			}
		}
		return array(
			'sort'	=>	$offset+1,
			'last_step'	=>	($offset>0?$list[$offset-1]['step']-$list[$offset]['step']:0),
		);
	}

	/**
	 * 获取个人的记录
	 * @author XYL
	 * @DateTime 2018-06-04
	 * @param    [type]     $user_id [description]
	 * @param    [type]     $type    [1日，2周，3月]
	 * @return   [type]              [description]
	 */
	public function get_record($user_id,$type)
	{
		$where = array(
			'user_id'	=>	$user_id,
		);
		if ($type==1) {
			$where['date>='] = date('Y-m-01');
			$where['date<='] = date('Y-m-d');
			$select = "DATE_FORMAT(date,'%Y.%m.%d') as time_detail,step,sort";
			$order_by = "time_detail desc";
			$table = $this->table;
		} elseif ($type==2) {
			$where['type'] = 1;
			$where['time>'] = date('Y W',strtotime('-12 week'));
			$select = "time, step,time_detail,sort";
			$order_by = "time desc";
			$table = 'rank';
		} elseif ($type==3) {
			$where['type'] = 2;
			$where['time>'] = date('Y-m',strtotime('-6 month'));
			$select = "time, step,time_detail,sort";
			$order_by = "time desc";
			$table = 'rank';
		}
		$list = $this->db->where($where)->select($select)->order_by($order_by)->get($table)->result_array();
		return $list;
	}

	/**
	 * 生成假数据
	 * @author XYL
	 * @DateTime 2018-06-08
	 * @return   [type]     [description]
	 */
	public function create_today_record()
	{
		$user = $this->hw_getList('user',array('del_status'=>0));
		$step = $distance = $cal = $time = array();
		for ($i=0; $i < count($user); $i++) { 
			$step_one = mt_rand(2000, 39999);
			$distance_one = floor($step_one/1333*1000);
			$cal[] = floor($distance_one*60*1.036/1000);
			$time[] = floor($distance_one/4000*60*60);
			$step[] = $step_one;
			$distance[] = $distance_one;
		}
		$new_step = $step;
		arsort($new_step);
		$num = 1;
		foreach ($new_step as $key => $value) {
			$data = array(
				'date'		=>	date('Y-m-d'),
				'user_id'	=>	$user[$key]['id'],
				'step'		=>	$value,
				'distance'		=>	$distance[$key],
				'cal'		=>	$cal[$key],
				'time'		=>	$time[$key],
				'is_online'	=>	1,
				'sort' 	=>	$num,
			);
			$num++;
			$this->update_record($user[$key]['id'],$value,$num);
			$this->hw_Insert($this->table,$data);
		}
	}

	/**
	 * 更新一个用户的记录
	 * @author XYL
	 * @DateTime 2018-06-08
	 * @param    [type]     $user_id [description]
	 * @param    [type]     $step    [description]
	 * @return   [type]              [description]
	 */
	public function update_user_step($user_id,$step)
	{
		$where = array(
			'user_id'	=>	$user_id,
			'date'	=>	date('Y-m-d'),
		);
		$distance = floor($step/1333*1000);
		$cal = floor($distance*60*1.036/1000);
		$time = floor($distance/4000*60*60);
		$info = $this->hw_getRecord($this->table,$where);
		$update = array(
			'step'		=>	$info['step']+$step,
			'distance'	=>	$info['distance']+$distance,
			'time'		=>	$info['time']+$time,
			'cal'		=>	$info['cal']+$cal,
			'update_time'	=>	date('Y-m-d H:i:s'),
		);
		$this->hw_Update($this->table,$update,$where);
		$step_list = $this->hw_getList($this->table,array('date'=>date('Y-m-d')),'*','step desc');
		$set_sort = array(
			'field'	=>	'sort',
			'field_edit'	=>	1,
		);
		$sort = 1;
		foreach ($step_list as $key => $value) {
			if ($value['sort']!=$key+1) {
				$set_sort['field_edit'] = $key+1;
				$this->hw_Update_set($this->table,array('id'=>$value['id']),$set_sort);
			}
			if ($value['user_id']==$user_id) {
				$sort = $key+1;
			}
		}
		$this->update_record($user_id,$step,$sort);
	}

	/**
	 * 更新记录
	 * @author XYL
	 * @DateTime 2018-06-08
	 * @param    [type]     $user_id [description]
	 * @param    [type]     $step    [description]
	 * @param    [type]     $sort    [description]
	 * @return   [type]              [description]
	 */
	public function update_record($user_id,$step,$sort)
	{
		$monday = date('Y.m.d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
		$sunday = date('Y.m.d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600));
		$week_num = date('Y W');
		$month_num = date('Y-m');
		$info = $this->hw_getRecord('rank',array('user_id'=>$user_id,'time'=>$week_num));
		// $this->db->trans_brgin();
		if (!empty($info)) {
			//更新记录
			$where_week = array(
				'user_id'	=>	$user_id,
				'time'	=>	$week_num,
			);
			$where_month = array(
				'user_id'	=>	$user_id,
				'time'	=>	$month_num,
			);
			$set = array(
				'field'     => 'step',
				'field_edit' => 'step + '.$step,
			);
			$this->hw_Update_set('rank',$where_week,$set);
			$this->hw_Update_set('rank',$where_month,$set);
		} else {
			//新增记录
			$week = array(
				'user_id'	=>	$user_id,
				'step'	=>	$step,
				'type'	=>	1,
				'time'	=>	$week_num,
				'time_detail'	=>	$monday.'-'.$sunday,
				'sort'	=>	$sort
			);
			$month = array(
				'user_id'	=>	$user_id,
				'step'	=>	$step,
				'type'	=>	2,
				'time'	=>	$month_num,
				'time_detail'	=>	date('Y.m'),
				'sort'	=>	$sort
			);
			$this->hw_Insert('rank',$week);
			$this->hw_Insert('rank',$month);
		}

		//重新排序
		$week_list = $this->hw_getList('rank',array('time'=>$week_num),'*','step desc');
		$month_list = $this->hw_getList('rank',array('time'=>$month_num),'*','step desc');
		$set_sort = array(
			'field'	=>	'sort',
			'field_edit'	=>	1,
		);
		foreach ($week_list as $key => $value) {
			if ($value['sort']!=$key+1) {
				$set_sort['field_edit'] = $key+1;
				$this->hw_Update_set('rank',array('id'=>$value['id']),$set_sort);
			}
		}
		foreach ($month_list as $key => $value) {
			if ($value['sort']!=$key+1) {
				$set_sort['field_edit'] = $key+1;
				$this->hw_Update_set('rank',array('id'=>$value['id']),$set_sort);
			}
		}
	}
}